<template>
  <a-card :bordered="false">
    <!-- 查询区域-start -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="refreshTable">
        <a-row :gutter="24">
          <a-col :md="6" :sm="12">
            <a-form-item label="标题">
              <a-input placeholder="请输入标题" v-model="queryParam.title" />
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="refreshTable" icon="search">查询</a-button>
            </span>
          </a-col>

        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-end -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="add" type="primary" icon="plus">添加案例</a-button>
    </div>

    <!-- table区域 -->
    <div>

      <s-table
        ref="table"
        rowKey="id"
        :columns="columns"
        :data="loadData"
      >
        <span slot="action" slot-scope="text, record">
          <template v-if="caseInfoStatusEnum.DISABLE.KEY === record.status || caseInfoStatusEnum.INIT.KEY === record.status">
            <a-divider type="vertical"/>
            <a-popconfirm title="确定发布吗?" @confirm="() => enable(record.id)">
              <a>发布</a>
            </a-popconfirm>
          </template>
          <template v-if="caseInfoStatusEnum.ENABLE.KEY === record.status">
            <a-divider type="vertical"/>
            <a-popconfirm title="确定下架吗?" @confirm="() => disable(record.id)">
              <a>下架</a>
            </a-popconfirm>
          </template>
          <a-divider type="vertical"/>
          <a @click="showComment(record)">查看评论</a>
          <template v-if="caseInfoStatusEnum.INIT.KEY === record.status || caseInfoStatusEnum.DISABLE.KEY === record.status">
            <a-divider type="vertical"/>
            <a @click="edit(record)">编辑</a>
          </template>
          <template v-if="caseInfoStatusEnum.ENABLE.KEY !== record.status">
            <a-divider type="vertical"/>
            <a-popconfirm title="确定删除吗?" @confirm="() => del(record.id)">
              <a>删除</a>
            </a-popconfirm>
          </template>
        </span>
      </s-table>
    </div>

    <case-info-modal ref="modal" @ok="refreshTable"></case-info-modal>

  </a-card>
</template>

<script>

import { ListMixin } from '@/mixins/ListMixin'
import { apiQueryCaseInfoList, apiEnableCaseInfo, apiDisableCaseInfo, apiDeleteCaseInfo } from '@/api/article'
import CaseInfoModal from './modules/CaseInfoModal'
import { CASE_INFO_STATUS_ENUM } from '@/store/mutation-types'

const columns = [
  {
    title: '标题',
    dataIndex: 'title',
    key: 'title'
  },
  {
    title: '描述',
    dataIndex: 'description',
    key: 'description'
  },
  {
    title: '状态',
    dataIndex: 'statusDesc',
    key: 'statusDesc'
  },
  {
    title: '操作',
    dataIndex: 'action',
    scopedSlots: { customRender: 'action' },
    align: 'center',
    width: 280
  }
]

export default {
  name: 'CaseInfoList',
  components: {
    CaseInfoModal
  },
  mixins: [ListMixin],
  data () {
    return {
      show: false,
      columns,
      loadData: parameter => {
        return apiQueryCaseInfoList(Object.assign(parameter, this.queryParam)).then(res => {
          return res
        })
      },
      caseInfoStatusEnum: CASE_INFO_STATUS_ENUM
    }
  },
  methods: {
    add () {
      this.$refs.modal.show('新增')
    },
    edit (record) {
      this.$refs.modal.show('修改', record)
    },
    showComment (record) {
      this.$refs.modal.show('查看评论', Object.assign({ showComment: true }, record))
    },
    enable (id) {
      apiEnableCaseInfo(id).then(res => {
        this.$message.success(res.msg)
      }).finally(() => {
        this.refreshTable()
      })
    },
    disable (id) {
      apiDisableCaseInfo(id).then(res => {
        this.$message.success(res.msg)
      }).finally(() => {
        this.refreshTable()
      })
    },
    del (id) {
      apiDeleteCaseInfo(id).then(res => {
        this.$message.success(res.msg)
      }).finally(() => {
        this.refreshTable()
      })
    }
  }
}
</script>

<style scoped>

</style>
